﻿Imports System.Data.SqlClient

Public Class clsKmsMensuales
    Private mintIdMensual As Integer
    Private mintIdAutobus As Integer
    Private mintMes As Integer
    Private mintAnio As Integer
    Private mdblKmsAcumulados As Double
    Private mdblKmsTrabajados As Double
    Private mdblKmsMuertos As Double
    Public Property IdMensual() As Integer
        Get
            Return mintIdMensual
        End Get
        Set(ByVal value As Integer)
            mintIdMensual = value
        End Set
    End Property
    Public Property IdAutobus() As Integer
        Get
            Return mintIdAutobus
        End Get
        Set(ByVal value As Integer)
            mintIdAutobus = value
        End Set
    End Property
    Public Property Mes() As Integer
        Get
            Return mintMes
        End Get
        Set(ByVal value As Integer)
            mintMes = value
        End Set
    End Property
    Public Property Anio() As Integer
        Get
            Return mintAnio
        End Get
        Set(ByVal value As Integer)
            mintAnio = value
        End Set
    End Property
    Public Property KmsAcumulados() As Double
        Get
            Return mdblKmsAcumulados
        End Get
        Set(ByVal value As Double)
            mdblKmsAcumulados = value
        End Set
    End Property
    Public Property KmsTrabajados() As Double
        Get
            Return mdblKmsTrabajados
        End Get
        Set(ByVal value As Double)
            mdblKmsTrabajados = value
        End Set
    End Property
    Public Property KmsMuertos() As Double
        Get
            Return mdblKmsMuertos
        End Get
        Set(ByVal value As Double)
            mdblKmsMuertos = value
        End Set
    End Property
    Public Sub New()
        mintIdMensual = 0
        mintIdAutobus = 0
        mintMes = 0
        mintAnio = 0
        mdblKmsAcumulados = 0
        mdblKmsTrabajados = 0
        mdblKmsMuertos = 0
    End Sub
    Public Sub Insertar()
        Dim sql As String = "Insert Into KmsMensuales (" & _
                            "IdAutobus," & _
                            "Mes," & _
                            "Anio," & _
                            "KmsAcumulados," & _
                            "KmsTrabajados," & _
                            "KmsMuertos)" & _
                            "VALUES (" & _
                            "@IdAutobus," & _
                            "@Mes," & _
                            "@Anio," & _
                            "@KmsAcumulados," & _
                            "@KmsTrabajados," & _
                            "@KmsMuertos)" & _
                            " SELECT SCOPE_IDENTITY()"

        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = CitraConnection

        Dim Command As New SqlCommand(sql, cnConn)

        Command.Parameters.AddWithValue("IdAutobus", mintIdAutobus)
        Command.Parameters.AddWithValue("Mes", mintMes)
        Command.Parameters.AddWithValue("Anio", mintAnio)
        Command.Parameters.AddWithValue("KmsAcumulados", mdblKmsAcumulados)
        Command.Parameters.AddWithValue("KmsTrabajados", mdblKmsTrabajados)
        Command.Parameters.AddWithValue("KmsMuertos", mdblKmsMuertos)

        cnConn.Open()




        Try
            IdMensual = Convert.ToInt32(Command.ExecuteScalar)
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Aviso")

        End Try

        If IdMensual > 0 Then
            cnConn.Close()
        Else
            MsgBox("No fue posible insertar el detalle mensual ", MsgBoxStyle.Exclamation, "Aviso")
            If cnConn.State = ConnectionState.Open Then
                cnConn.Close()
            End If
        End If
    End Sub
    Public Sub Actualizar()

        Dim strSqlQry As String
        Dim cnConn As New SqlConnection

        cnConn.ConnectionString = CitraConnection
        strSqlQry = "Update KmsMensuales Set " & _
                    "KmsAcumulados=@KmsAcumulados, " & _
                    "KmsTrabajados=@KmsTrabajados," & _
                    "KmsMuertos=@KmsMuertos " & _
                    "Where IdMensuales=@IdMensuales"


        Dim Command As New SqlCommand(strSqlQry, cnConn)

        Command.Parameters.AddWithValue("IdMensuales", mintIdMensual)
        Command.Parameters.AddWithValue("IdAutobus", mintIdAutobus)
        Command.Parameters.AddWithValue("KmsAcumulados", mdblKmsAcumulados)
        Command.Parameters.AddWithValue("KmsTrabajados", mdblKmsTrabajados)
        Command.Parameters.AddWithValue("KmsMuertos", mdblKmsMuertos)
        cnConn.Open()

        Dim RowsAffected As Integer


        Try
            RowsAffected = Command.ExecuteNonQuery

        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Aviso")

        End Try

        If RowsAffected > 0 Then
            cnConn.Close()
        Else
            MsgBox("No fue posible actualizar el liquidación ", MsgBoxStyle.Exclamation, "Aviso")
            If cnConn.State = ConnectionState.Open Then
                cnConn.Close()
            End If
        End If



    End Sub
End Class
